// ===============================================
// app_router
//
// Create by Will on 2024/12/5 01:25
// Copyright @flutter_base.All rights reserved.
// ===============================================
import 'package:flutter/widgets.dart';
import 'package:go_router/go_router.dart';

import '../core/toast.dart';
import '../screens/app_main/app_main_scaffold.dart';
import '../screens/home/home_page.dart';
import '../screens/profile/profile/profile_page.dart';
import '../screens/profile/settings/settings_page.dart';
import 'app_router_name.dart';

const ValueKey<String> _scaffoldKey = ValueKey<String>('App Main Scaffold');

final GoRouter appRouter = GoRouter(
    routes: <GoRoute>[
      GoRoute(
        path: '/',
        redirect: (_, __) => AppRouterName.home,
      ),
      GoRoute(
        path: AppRouterName.home,
        pageBuilder: (context, state) {
          return FadeTransitionPage(
            key: _scaffoldKey,
            child: const AppMainScaffold(
              selectedTab: ScaffoldTab.home,
              child: HomePage(),
            ),
          );
        },
      ),
      GoRoute(
        path: AppRouterName.profile,
        pageBuilder: (context, state) => FadeTransitionPage(
          key: _scaffoldKey,
          child: const AppMainScaffold(
            selectedTab: ScaffoldTab.profile,
            child: ProfilePage(),
          ),
        ),
        routes: <GoRoute>[
          GoRoute(
            path: 'settings',
            builder: (context, state) {
              return const SettingsPage();
            },
          ),
        ],
      ),
    ],
    // redirect: _guard,
    // refreshListenable: _auth,
    debugLogDiagnostics: true,
    observers: <NavigatorObserver>[CoreKitToastNavigatorObserver()]);

/// A page that fades in an out.
class FadeTransitionPage extends CustomTransitionPage<void> {
  /// Creates a [FadeTransitionPage].
  FadeTransitionPage({
    required LocalKey super.key,
    required super.child,
  }) : super(
          transitionsBuilder: (context, animation, secondaryAnimation, child) =>
              FadeTransition(
            opacity: animation.drive(_curveTween),
            child: child,
          ),
        );

  static final CurveTween _curveTween = CurveTween(curve: Curves.easeIn);
}
